import Joi from 'joi';
import debugCheck from '@kuiper-helpers/debug-check';
import * as validation from '../../common/validation';
import extractQueryFieldByJoi from '../helpers/extractQueryFieldByJoi';
import findManyFilter from '../../common/validation/findManyFilter';
import joiOmit from '../helpers/joiOmit';

export const pagination = {
	tags: ['api', 'alertReceiver'],
	description: '告警人列表-分页查看',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		// payload: Joi.object({
		// 	page: Joi.number().default(1).description('当前页'),
		// 	perPage: Joi.number().default(10).description('每页条目数'),
		// 	filter: findManyFilter.alertReceiver,
		// }),
	},

	response: {
		// schema: Joi.object({
		// 	data: Joi.object({
		// 		AlertReceiverPagination: Joi.object({
		// 			count: Joi.number().description('总条数'),
		// 			pageInfo: Joi.object({
		// 				currentPage: Joi.number().description('当前页数'),
		// 				perPage: Joi.number().description('每页条数'),
		// 				pageCount: Joi.number().description('总页数'),
		// 				itemCount: Joi.number().description('总条数'),
		// 				hasNextPage: Joi.boolean().description('是否有下一页'),
		// 				hasPreviousPage: Joi.boolean().description('是否有上一页'),
		// 			}),
		// 			items: Joi.array().items(validation.alertReceiverWithId),
		// 		}),
		// 	}),
		// }).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const {
					payload: { page, perPage, filter },
				} = request;

				return JSON.stringify({
					variables: { page, perPage, filter },
					// #region query
					query: `
						query($page: Int = 1, $perPage: Int = 10, $filter: FilterFindManyAlertReceiverInput, $sort: SortFindManyAlertReceiverInput = _ID_DESC) {
							AlertReceiverPagination(page: $page, perPage: $perPage, filter: $filter, sort: $sort) {
								count
								pageInfo {
									currentPage
									perPage
									pageCount
									itemCount
									hasNextPage
									hasPreviousPage
								}
								items {
									_id
									uid
									name
									phone
									email
									enable
									wechat
									isDefault
								}
							}
						}`,
					// #endregion
				});
			},
		},
	},
};

export const findMany = {
	tags: ['api', 'alertReceiver'],
	description: '告警人列表',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		query: findManyFilter.alertReceiver.unknown(),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				AlertReceiverMany: Joi.array().items(validation.alertReceiverWithId),
			}),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const { query } = request;

				return JSON.stringify({
					variables: {
						filter: extractQueryFieldByJoi({
							query,
							schema: validation.alertReceiverWithId,
						}),
					},
					// #region query
					query: `
						query($filter: FilterFindManyAlertReceiverInput) {
							AlertReceiverMany(filter: $filter, limit: 0) {
								_id
								uid
								name
								phone
								email
								enable
								wechat
							}
						}`,
					// #endregion
				});
			},
		},
	},
};

export const createOne = {
	tags: ['api', 'alertReceiver'],
	description: '创建告警人',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		payload: validation.alertReceiver,
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				AlertReceiverCreateOne: Joi.object({
					recordId: Joi.string(),
					record: validation.alertReceiverWithId,
				}).label('AlertReceiverCreateOne'),
			}).label('Data'),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const { payload: record } = request;

				return JSON.stringify({
					variables: {
						record,
					},
					// #region query
					query: `
						mutation($record: CreateOneAlertReceiverInput!) {
							AlertReceiverCreateOne(record: $record) {
								recordId
								record {
									_id
									name
									email
									phone
									enable
									wechat
									isDefault
								}
							}
						}`,
					// #endregion
				});
			},
		},
	},
};

export const updateById = {
	tags: ['api', 'alertReceiver'],
	description: '更新告警人',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		params: Joi.object({
			alertReceiverId: Joi.string().required(),
		}),
		payload: joiOmit(validation.alertReceiver),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				AlertReceiverUpdateById: Joi.object({
					recordId: Joi.string().description('更新记录的ID'),
				}),
			}),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const {
					payload,
					params: { alertReceiverId },
				} = request;

				return JSON.stringify({
					variables: {
						record: payload,
						filter: {
							uid: payload.uid,
							_id: alertReceiverId,
						},
					},
					// #region query
					query: `
						mutation($filter: FilterUpdateOneAlertReceiverInput!, $record: UpdateOneAlertReceiverInput!) {
							AlertReceiverUpdateById: AlertReceiverUpdateOne(filter: $filter, record: $record) {
								recordId
							}
						}`,
					// #endregion
				});
			},
		},
	},
};

export const removeById = {
	tags: ['api', 'alertReceiver'],
	description: '按ID删除告警人',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		params: Joi.object({
			alertReceiverId: Joi.string().required(),
		}),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				AlertReceiverRemoveMany: Joi.object({
					numAffected: Joi.number().description('删除条数'),
				}).label('AlertReceiverRemoveMany'),
			}).label('Data'),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const {
					params: { alertReceiverId },
				} = request;

				return JSON.stringify({
					variables: { alertReceiverId },
					// #region query
					query: `
						mutation($alertReceiverId: MongoID!) {
							AlertReceiverRemoveMany(filter: { _ids: [$alertReceiverId] }) {
								numAffected
							}
						}`,
					// #endregion
				});
			},
		},
	},
};

export const removeMany = {
	tags: ['api', 'alertReceiver'],
	description: '批量删除告警人',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		payload: Joi.array()
			.items(Joi.string().description('告警人ID'))
			.required()
			.description('告警人ID列表'),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				AlertReceiverRemoveMany: Joi.object({
					numAffected: Joi.number().description('删除条数'),
				}).label('AlertReceiverRemoveMany'),
			}).label('Data'),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const { payload } = request;

				return JSON.stringify({
					variables: { _ids: payload },
					// #region query
					query: `
						mutation($_ids: [MongoID]) {
							AlertReceiverRemoveMany(filter: { _ids: $_ids }) {
								numAffected
							}
						}`,
					// #endregion
				});
			},
		},
	},
};

export const count = {
	tags: ['api', 'alertReceiver'],
	description: '告警人数量统计',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		payload: Joi.object({
			filter: findManyFilter.alertReceiver,
		}),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				AlertReceiverCount: Joi.number().description('数量'),
			}),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const {
					payload: { filter },
				} = request;

				return JSON.stringify({
					variables: { filter },
					// #region query
					query: `
						query($filter: FilterCountAlertReceiverInput) {
							AlertReceiverCount(filter: $filter)
						}`,
					// #endregion
				});
			},
		},
	},
};
